#include "mainwindow.h"
#include "./ui_mainwindow.h"

#include <QFileDialog>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    initUI();

    progressform = new ProgressForm;
    ui->stackedWidget->addWidget(progressform);
    // progressform->LoadProgressFromConf("defaut_launch.txt");
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initUI()
{
    im1 = new QStringListModel;
    im2 = new QStringListModel;
}


void populateTableWithWRLFiles(QTableWidget *tableWidget, const QStringList &filePaths) {
    // 清除现有内容
    tableWidget->clear();
    tableWidget->setRowCount(0);

    // 设置表头
    tableWidget->setColumnCount(2);
    tableWidget->setHorizontalHeaderLabels({"文件名", "路径"});

    // 遍历文件路径列表
    for (const QString &filePath : filePaths) {
        QFileInfo fileInfo(filePath);

        // 插入新行
        int row = tableWidget->rowCount();
        tableWidget->insertRow(row);

        // 填充文件名和路径
        QTableWidgetItem *fileNameItem = new QTableWidgetItem(fileInfo.fileName());
        QTableWidgetItem *filePathItem = new QTableWidgetItem(fileInfo.absoluteFilePath());

        tableWidget->setItem(row, 0, fileNameItem);
        tableWidget->setItem(row, 1, filePathItem);
    }

    // 调整列宽适应内容
    tableWidget->resizeColumnsToContents();
}


void MainWindow::on_pb_next_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
}


void MainWindow::on_pb_last_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
}


void MainWindow::on_pb_import1_clicked()
{
    auto list = QFileDialog::getOpenFileNames(nullptr, "选择文件", QDir::homePath(),
                                             "wrl文件 (*.wrl);;所有文件 (*)");

    if(list.empty()){
        return;
    }else{
        /** 得到所有的wrl，并生成listmodel **/
        populateTableWithWRLFiles(ui->tableWidget,list);
    }

}


void MainWindow::on_pb_import2_clicked()
{
    auto list = QFileDialog::getOpenFileNames(nullptr, "选择文件", QDir::homePath(),
                                             "wrl文件 (*.wrl);;所有文件 (*)");

    if(list.empty()){
        return;
    }else{
        /** 得到所有的wrl，并生成listmodel **/
        populateTableWithWRLFiles(ui->tableWidget_2,list);
    }
}


std::vector<std::string> CreateJson(QTableWidget *tableWidget,int station) {
    std::vector<std::string> ret;
    int rowCount = tableWidget->rowCount();

    // 遍历每一行，获取第一列的内容并打印
    for (int row = 0; row < rowCount; ++row) {
        QTableWidgetItem *item = tableWidget->item(row, 1); // 第一列为索引0

        if (item != nullptr) { // 检查该项是否存在
            auto wrl = item->text().toStdString();
            ret.push_back(wrl);
        } else {
            std::cout << "Row " << row << ": (null)" << std::endl;
        }
    }
    return ret;
}


std::vector<std::pair<int, std::string>> interleaveVectors(
    const std::vector<std::string>& A,
    const std::vector<std::string>& B
) {
    std::vector<std::pair<int, std::string>> result;
    size_t minSize = std::min(A.size(), B.size());

    for (size_t i = 0; i < minSize; ++i) {
        result.emplace_back(1, A[i]); // A 中的元素标记为 1
        result.emplace_back(2, B[i]); // B 中的元素标记为 2
    }

    // 如果 A 或 B 更长，添加剩余的元素
    for (size_t i = minSize; i < A.size(); ++i) {
        result.emplace_back(1, A[i]);
    }

    for (size_t i = minSize; i < B.size(); ++i) {
        result.emplace_back(2, B[i]);
    }

    return result;
}

void MainWindow::on_create_clicked()
{
    auto l1 = CreateJson(ui->tableWidget,1);
    auto l2 = CreateJson(ui->tableWidget_2,2);

    auto l3 = interleaveVectors(l1,l2);
    std::string TargetJsonDir = "TargetWRL";

    std::ofstream outFile("jsonList.txt");

    if (!outFile.is_open()) {
        std::cerr << "无法打开文件: " << "jsonList.txt" << std::endl;
        return;
    }

    int index = 0;
    for (const auto& p : l3) {
        int station = p.first;
        index ++;
        std::string wrl =  p.second;
        // detach :YES
        outFile << "./Json.AppImage" << ',' << "NO," << wrl << ','<< index << ','<< station << '\n';
    }

    outFile.close();

    progressform->LoadProgressFromConf("jsonList.txt");

    progressform->LoadProgressFromConf("defaut_launch.txt");
}


void MainWindow::on_pb_loadALL_clicked()
{
    QStringList l1,l2;
    auto list = QFileDialog::getOpenFileNames(nullptr, "选择文件", QDir::homePath(),
                                             "wrl文件 (*.wrl);;所有文件 (*)");
    if(list.empty()){
        return;
    }else{
        //todo

        populateTableWithWRLFiles(ui->tableWidget_2,l2);
        populateTableWithWRLFiles(ui->tableWidget,l1);
    }
}

